﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using PayPal;
using PayPal.Api.Payments;
using Newtonsoft.Json.Linq;
using RestApiSample;


public partial class Default2 : System.Web.UI.Page
{
    private PayPal.APIService api;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        Item item = new Item();
        item.name = "month";
        item.currency = "USD";
        item.price = "10";
        item.quantity = "12";
        item.sku = "sku";

        List<Item> listItem = new List<Item>();
        listItem.Add(item);
        ItemList itemList = new ItemList();
        itemList.items = listItem;

        Address billingAddress = new Address();
        billingAddress.city = "Johnstown";
        billingAddress.country_code = "US";
        billingAddress.line1 = "52 N Main ST";
        billingAddress.postal_code = "43210";
        billingAddress.state = "OH";

        CreditCard crdtCard = new CreditCard();
        crdtCard.billing_address = billingAddress;
        crdtCard.cvv2 = "874";
        crdtCard.expire_month = 11;
        crdtCard.expire_year = 2018;
        crdtCard.first_name = "Joe";
        crdtCard.last_name = "Shopper";
        crdtCard.number = "4417119669820331";
        crdtCard.type = "visa";

        Details details = new Details();
        details.shipping = "1";
        details.subtotal = "5";
        details.tax = "1";

        Amount amnt = new Amount();
        amnt.currency = "USD";

        amnt.total = "7";
        amnt.details = details;

        Transaction tran = new Transaction();
        tran.amount = amnt;
        tran.description = "This is the payment transaction description.";
        tran.item_list = itemList;

        List<Transaction> transactions = new List<Transaction>();
        transactions.Add(tran);

        FundingInstrument fundInstrument = new FundingInstrument();
        fundInstrument.credit_card = crdtCard;

        List<FundingInstrument> fundingInstrumentList = new List<FundingInstrument>();
        fundingInstrumentList.Add(fundInstrument);

        Payer payr = new Payer();
        payr.funding_instruments = fundingInstrumentList;
        payr.payment_method = "credit_card";

        Payment pymnt = new Payment();
        pymnt.intent = "sale";
        pymnt.payer = payr;
        pymnt.transactions = transactions;

        try
        {
            APIContext apiContext = Configuration.GetAPIContext();
            Payment createdPayment = pymnt.Create(apiContext);
            context.Items.Add("ResponseJson", JObject.Parse(createdPayment.ConvertToJson()).ToString(Formatting.Indented));
        }
        catch (PayPal.Exception.PayPalException ex)
        {
            context.Items.Add("Error", ex.Message);
        }
        context.Items.Add("RequestJson", JObject.Parse(pymnt.ConvertToJson()).ToString(Formatting.Indented));

        Server.Transfer("~/Response.aspx");

    }
}